package com.lanou3g.hw0426;

import java.util.ArrayList;
import java.util.Scanner;

public class Util {


    public boolean login(User user) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String username = scanner.nextLine();
        boolean usernameRight = username.equals(user.getUsername());

        System.out.println("请输入密码:");
        String pwd = scanner.nextLine();
        boolean pwdRight = pwd.equals(user.getPwd());

        return usernameRight && pwdRight;
    }

    public boolean login(ArrayList<User> users) {
        boolean canLogin = false;
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String username = scanner.nextLine();

        System.out.println("请输入密码:");
        String pwd = scanner.nextLine();
        for (User user : users) {

            if (username.equals(user.getUsername()) && pwd.equals(user.getPwd())) {
                canLogin = true;
            }
        }
        return canLogin;
    }

    public User register() {
        User user = new User();
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("请输入用户名");
            String username = scanner.nextLine();
            boolean isStartWithNum = false;
            for (int i = 0; i < 10; i++) {
                String num = String.valueOf(i);
                if (username.startsWith(num)) {

                    isStartWithNum = true;
                }
            }
            if (!isStartWithNum) {
                user.setUsername(username);
                break;
            }
        }

        String pwd = null;
        while (true) {
            System.out.println("请输入密码:");
            pwd = scanner.nextLine();
            boolean hasNum = false;
            for (int i = 0; i < 10; i++) {
                String num = String.valueOf(i);
                if (pwd.contains(num)) {
                    hasNum = true;
                }
            }
            if (!hasNum) {
                System.out.println("密码需要包含数字");
                continue;
            }
            boolean hasLowerCase = false;
            for (char c = 'a'; c <= 'z'; c++) {
                String letter = String.valueOf(c);
                if (pwd.contains(letter)) {
                    hasLowerCase = true;
                }
            }

            if (!hasLowerCase) {
                System.out.println("密码需要包含小写字母");
                continue;
            }

            boolean hasUpperCase = false;
            for (char c = 'A'; c <= 'Z'; c++) {
                String letter = String.valueOf(c);
                if (pwd.contains(letter)) {
                    hasUpperCase = true;
                }
            }
            if (!hasUpperCase) {
                System.out.println("密码需要包含大写字母");
                continue;
            }
            break;
        }

        while (true) {
            System.out.println("再次输入密码:");
            String rePwd = scanner.nextLine();
            if (pwd.equals(rePwd)) {
                user.setPwd(pwd);
                break;
            } else {
                System.out.println("密码和确认密码不一致");
            }
        }
        return user;
    }
}